<?php
require_once(dirname(__FILE__) . '/PortalUtil.class.php');

class Mac extends PortalUtil {
	public $tbl = Constant::tbl_wifi_mac;

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function _where($request) {
		$uid_s = $request['uid_s'];
	
		$where = " WHERE 1=1";
		
		if (!empty($uid_s)) {
			$where .= " AND uid = '" . $uid_s . "'";
		}
	
		// $where .= $this->_where_default($request);
		// $where = $this->_where_aa($where);
	
		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		if (empty($_SESSION['uid'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$request['uid_s'] = $_SESSION['uid'];
		
		$this->nav['home'] = true;
		
		$this->findPara($controller, $request);
		
		// $this->orderby_s = "id";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$pager_option = array (
			'db' => $db,
			'sql' => $sql,
			'PageSize' => 8,
			'CurrentPageID' => $request['p'],
			'numItems' => $request['n']
		);
		
		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();
		
		if ($pager->number > 0) {
		
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
		
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		$controller->display($request, 'mac/index');
		return true;
	}
	
	function _view($id = 0, $row = null) {
	
		$row['black'] = $this->_is_black($row['username'], $row['mac']);
	
		return $row;
	}
	
	function _is_black($uid, $mac) {
		require_once 'Black.class.php';
		
		$aa = new Black();
		
		return $aa->_is_black($uid, $mac);
	}
	
	function black($controller, $request) {
		$this->pretreat($controller);
		
		if (empty($_SESSION['uid'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$user = $_SESSION['user'];
		
		$mac = $request['mac'];
		$status = $request['status'];
		
		require_once 'Black.class.php';
		
		$aa = new Black();
		
		$aa->_update($user, $mac, $status);
		
		return $this->layout($controller, $request);
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);
	
		if (empty($_SESSION['uid'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$uid = $_SESSION['uid'];
		$mac = $request['mac'];
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE uid = '%s' AND mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $uid, $mac);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			
			$controller->assign_by_ref('result', $row);
		}
	
		$controller->display($request, 'mac/edit');
		return true;
	}
	
	function editPost($controller, $request) {
		
		$id = $request['id'];
		$mac = $request['mac'];
		$name = $request['name'];
		
		$ret = array('status'=>1);
		
		if (empty($name)) {
			$ret['status'] = 2;
			echo json_encode($ret);
			return false;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET name = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $id);
		$db->query($sql);
		
		echo json_encode($ret);
		return true;
	}
	
	function _by_mac($uid, $mac) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE uid = '%s' AND mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $uid, $mac);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row;
		}
		
		return false;
	}
	
	function _update1($request) {
		
		$gw_id = $request['gw_id'];
		$mac = $request['mac'];
		
		$content = json_encode($request);
		
		$ip	= $_SERVER['REMOTE_ADDR'];
		$ua = $_SERVER['HTTP_USER_AGENT'];
		
		if (empty($mac) || empty($ua)) {
			return;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mac);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '2') {
				return;
			}
			
			$fmt = "UPDATE %s SET ua = '%s', modifytime = NOW() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $ua, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(gw_id, content, mac, ip, ua, createtime, modifytime) 
					VALUES('%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
			$sql = sprintf($fmt, $this->tbl, $gw_id, $content, $mac, $ip, $ua);
		}
		
		$db->query($sql);
	}
	
	function _update2($request, $user) {
	
		$office = $user['office'];
		$dept = $user['dept'];
		$emp = $user['emp'];
		
		$uid = $user['id'];
		
		$mac = $request['mac'];
	
		// $content = json_encode($request);
	
		if (empty($mac) || empty($uid)) {
			return;
		}
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', uid = '%s', 
				modifytime = NOW() WHERE mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $uid, $mac);
		$db->query($sql);
	}
}
?>
